home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Parrot 1.0b1 / source / main.c < prev    next >
C/C++ Source or Header  |  1993-09-06  |  7KB  |  287 lines

  1. /*------------------------------------------------------------------------------
  2. # Main for Confusion
  3. #
  4. #    Copyright © 1992 Bernard Bernstein
  5. #
  6. #    taken from some background-only sample code and other sample code shells
  7. ------------------------------------------------------------------------------*/
  8.  
  9. #include <memory.h>
  10. #include <appleevents.h>
  11. #include <events.h>
  12. #include <types.h>
  13. #include <gestaltequ.h>
  14.  
  15. #include <SoundInput.h>
  16. #include <Sound.h>
  17.  
  18. #include "Confusion.h"
  19.  
  20. /* variables */
  21. Boolean    gInBackground;
  22. Boolean gQuit = false;
  23. EventRecord gERecord;
  24. Boolean gHasAppleEvents;
  25. unsigned long gMySleep = 60;
  26.  
  27. struct AEinstalls {
  28.     AEEventClass theClass;
  29.     AEEventID theEvent;
  30.     EventHandlerProcPtr theProc;
  31. };
  32. typedef struct AEinstalls AEinstalls;
  33.  
  34. /* prototypes for this file */
  35. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  36. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  37. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  38. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  39. void DoHighLevel(EventRecord *AERecord);
  40. void InitAEStuff(void);
  41. void DoEvent(EventRecord *event);
  42. void DoMenuCommand(long menuResult);
  43. void AdjustMenus( void );
  44. void Initialize(void);
  45.  
  46. main()
  47. {
  48.     Boolean gotEvent;
  49.     
  50.     Initialize();
  51.     InitSoundConfusion();
  52.    
  53.     while (gQuit == false) {
  54.         gotEvent = WaitNextEvent(everyEvent, &gERecord, gMySleep, 0);
  55.         if (gotEvent)
  56.             {
  57.             DoEvent(&gERecord);
  58.             }
  59.         if (!gQuit)
  60.             {
  61.             ConfuseSound();
  62.             }
  63.     }
  64.     StopConfusion();
  65.     ExitToShell();
  66. }
  67.  
  68. void DoEvent(EventRecord *event)
  69. {
  70.     short        part;
  71.     WindowPtr    window;
  72.     Boolean        hit;
  73.     char        key;
  74.  
  75.     switch ( event->what ) {
  76.         case mouseDown:
  77.             part = FindWindow(event->where, &window);
  78.             switch ( part ) {
  79.                 case inMenuBar:                /* process a mouse menu command (if any) */
  80.                     AdjustMenus();
  81.                     DoMenuCommand(MenuSelect(event->where));
  82.                     break;
  83.                 case inSysWindow:            /* let the system handle the mouseDown */
  84.                     SystemClick(event, window);
  85.                     break;
  86.             }
  87.             break;
  88.         case keyDown:
  89.         case autoKey:                        /* check for menukey equivalents */
  90.             key = event->message & charCodeMask;
  91.             if ( event->modifiers & cmdKey )            /* Command key down */
  92.                 if ( event->what == keyDown ) {
  93.                     AdjustMenus();                        /* enable/disable/check menu items properly */
  94.                     DoMenuCommand(MenuKey(key));
  95.                 }
  96.             break;
  97.         case osEvt:
  98.             switch ((event->message >> 24) & 0x0FF) {        /* high byte of message */
  99.                 case suspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  100.                     gInBackground = (event->message & resumeFlag    ) == 0;
  101.                     break;
  102.             }
  103.             break;
  104.         case kHighLevelEvent:
  105.             if (gHasAppleEvents)
  106.                 DoHighLevel(&gERecord);
  107.             break;
  108.     }
  109. } /*DoEvent*/
  110.  
  111.  
  112. void DoMenuCommand(long menuResult)
  113. {
  114.     short        menuID;                /* the resource ID of the selected menu */
  115.     short        menuItem;            /* the item number of the selected menu */
  116.     short        itemHit;
  117.     Str255        daName;
  118.     short        daRefNum;
  119.     Boolean        handledByDA;
  120.  
  121.     menuID = HiWord(menuResult);    /* use macros for efficiency to... */
  122.     menuItem = LoWord(menuResult);    /* get menu item number and menu number */
  123.     switch ( menuID ) {
  124.         case mApple:
  125.             switch ( menuItem ) {
  126.                 case iAbout:        /* bring up alert for About */
  127.                     DoAbout();
  128.                     break;
  129.                 default:            /* all non-About items in this menu are DAs */
  130.                     GetItem(GetMHandle(mApple), menuItem, daName);
  131.                     daRefNum = OpenDeskAcc(daName);
  132.                     break;
  133.             }
  134.             break;
  135.         case mFile:
  136.             switch ( menuItem ) {
  137.                 case iQuit:
  138.                     gQuit = true;
  139.                     break;
  140.             }
  141.             break;
  142.         case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  143.             handledByDA = SystemEdit(menuItem-1);    /* since we don’t do any Editing */
  144.             break;
  145.     }
  146.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  147. } /*DoMenuCommand*/
  148.  
  149.  
  150. void AlertUser(OSErr err, short errID)
  151. {
  152.     short        itemHit;
  153.     Str255        errStr;
  154.     Str255        description;
  155.     long        lErr = (long)err;
  156.     DialogPtr    dlg;
  157.     
  158.     SetCursor(&qd.arrow);
  159.     GetIndString(description, rErrStrings, errID);
  160.     NumToString(lErr, errStr);
  161.     
  162.     ParamText(errStr, description, "\p", "\p");
  163.     dlg = GetNewDialog(rErrAlert, NULL, (WindowPtr)-1L);
  164.     ModalDialog(NULL, &itemHit);
  165.     DisposDialog(dlg);
  166.     
  167.     gQuit = true;
  168. } /* AlertUser */
  169.  
  170. void AlertUserStr(Str255 description)
  171. {
  172.     short        itemHit;
  173.     DialogPtr    dlg;
  174.     
  175.     SetCursor(&qd.arrow);
  176.  
  177.     ParamText("\pnone", description, "\p", "\p");
  178.     dlg = GetNewDialog(rErrAlert, NULL, (WindowPtr)-1L);
  179.     ModalDialog(NULL, &itemHit);
  180.     DisposDialog(dlg);
  181. } /* AlertUser */
  182.  
  183. void AdjustMenus( void )
  184. {
  185.  
  186. }
  187.  
  188.  
  189. void Initialize()
  190. {
  191.     Handle        menuBar;
  192.     EventRecord event;
  193.     short        count;
  194.  
  195.     gInBackground = false;
  196.  
  197.     InitGraf((Ptr) &qd.thePort);
  198.     InitFonts();
  199.     InitWindows();
  200.     InitMenus();
  201.     TEInit();
  202.     InitDialogs(nil);
  203.     InitCursor();
  204.  
  205.     for (count = 1; count <= 3; count++)
  206.         EventAvail(everyEvent, &event);
  207.  
  208.     MaxApplZone();
  209.     
  210.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  211.     SetMenuBar(menuBar);                    /* install menus */
  212.     DisposHandle(menuBar);
  213.     AddResMenu(GetMHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  214.     DrawMenuBar();
  215.     
  216.     InitAEStuff();
  217. }
  218.  
  219.  
  220. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  221. /* installs our event handlers. */
  222. /* if the AEM isn't around, we bail. */
  223. void InitAEStuff(void)
  224. {
  225.     static AEinstalls HandlersToInstall[] =  {
  226.          {
  227.             kCoreEventClass, kAEOpenApplication, AEOpenHandler
  228.         },  {
  229.             kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
  230.         },  {
  231.             kCoreEventClass, kAEQuitApplication, AEQuitHandler
  232.         },  {
  233.             kCoreEventClass, kAEPrintDocuments, AEPrintHandler
  234.         }, 
  235.         /* The above are the four required AppleEvents. */
  236.     };
  237.     
  238.     OSErr aevtErr = noErr;
  239.     long aLong = 0;
  240.     Boolean gHasAppleEvents = false;
  241.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  242.     if (gHasAppleEvents) {
  243.         register qq;
  244.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  245.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  246.                                             HandlersToInstall[qq].theProc, 0, false);
  247.             if (aevtErr) {
  248.                 AlertUser(aevtErr, iAEErr);
  249.             }
  250.         }
  251.     }
  252. }
  253. /* end InitAEStuff */
  254.  
  255. void DoHighLevel(EventRecord *AERecord)
  256. {
  257.     AEProcessAppleEvent(AERecord);
  258. }
  259.  
  260. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  261. {
  262. #pragma unused (messagein,reply,refIn)
  263.     return(noErr);
  264. }
  265.  
  266. /* end AEOpenHandler */
  267.  
  268. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  269. {
  270. #pragma unused (reply, refIn,messagein)
  271.     return(errAEEventNotHandled);
  272. }
  273.  
  274. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  275. {
  276. #pragma unused (reply,refIn,messagein)
  277.     return(errAEEventNotHandled);
  278. }
  279.  
  280. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  281. {
  282. #pragma unused (messagein,refIn,reply)
  283.     extern Boolean Quit;
  284.     gQuit = true;
  285.     return(noErr);
  286. }
  287.